﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Se7en.Admin.Common;
using Se7en.Admin.Model;
using Se7en.Admin.Business;
using ERMSys.Framework.Utilities;

namespace Se7en.Admin.Web.Controls
{
    public partial class RoleList : UserControlBase
    {
        #region Properties

        public bool MultipleSelect { get; set; }

        public List<int> SelectedRoleIds
        {
            get
            {
                return grdRoleList.SelectedKeys.Select(k => k.ConvertToInt32()).ToList(); 
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            grdRoleList.EnableMultiSelect = this.MultipleSelect;
        }

        protected void grdRoleList_Refresh(object sender, EventArgs e)
        {
            BindRoles();
        }

        protected void grdRoleList_PageIndexChanged(object sender, EventArgs e)
        {
            BindRoles();
        }

        #endregion

        #region Methods

        public void BindRoles()
        {
            int totalRecord = 0;
            List<Role> roles = DBContext.Role_GetAll(0, "", grdRoleList.PageIndex, grdRoleList.PageSize, out totalRecord);
            grdRoleList.DataBind(roles, totalRecord);
        }

        public void ClearSelectedKeys()
        {
            grdRoleList.ClearSelectedKeys();
        }

        protected bool GetIsDefaultRole(object roleId)
        {
            Role role = DBContext.Role_GetRoleById(roleId.ConvertToInt32());
            return role == null ? false : role.IsDefault;
        }

        protected string GetRoleType(object objRoleType)
        {
            return EnumHelper.GetDescription((RoleType)objRoleType);
        }

        #endregion
    }
}